package han.Chensing.CCirnosLibrary;

import java.io.File;
import java.util.Scanner;

import han.Chensing.CCirnosLibrary.command.CommandHandler;
import han.Chensing.CCirnosLibrary.command.CommandResolver;
import han.Chensing.CCirnosLibrary.composer.CCirnoLibraryComposer;
import han.Chensing.CCirnosLibrary.func.NodeBatchDownloadCallback;
import han.Chensing.CCirnosLibrary.map.ItemDecInfo;
import han.Chensing.CCirnosLibrary.map.ItemNode;

public class TestMain {
    public static void main(String[] args) {
        compose();
        //download();
        console();
    }

    public static void console(){
        P.i("Welcome to use CCirnos Library console");
        P.i("Initializing console");
        CommandResolver commandResolver=new CommandResolver();
        CommandHandler commandHandler=new CommandHandler(commandResolver);
        Scanner scanner=new Scanner(System.in);
        P.i("Done");
        int result=0;
        do {
            String s1 = String.valueOf(result);
            if (s1.length() == 1)
                s1 = " " + s1;
            System.out.printf("%s%s%s: ", s1,
                    commandResolver.loaded?
                    commandResolver.library.isLocal()?"#":"$":" ",commandResolver.getNowPath());
            String s = scanner.nextLine();
            result = commandHandler.invoke(s);
        } while (result != -9);
        scanner.close();
    }

    public static void download(){
        try {
            CCirnoLibrary cCirnoLibrary = new CCirnoLibrary();
            cCirnoLibrary.fetchMenuList();

            ItemNode rootNode = cCirnoLibrary.getItemMap().getRootNode();
            rootNode.setAllItemDecInfo(new ItemDecInfo(){{
                setP("1919810");
            }});
            cCirnoLibrary.getItemMap().getRootNode().printMap();
            cCirnoLibrary.downloadListNode(rootNode, new File("E:\\CIRNOS'DOC\\GITS\\ccirnos-library\\test\\"),
                    new NodeBatchDownloadCallback() {
                        @Override
                        public void onProgressUpdate(ItemNode nowNode) {
                            System.out.println("D:"+nowNode.getName()+" - "+nowNode.getLink());
                        }
                        @Override
                        public void onPartProgressUpdate(int blockNo, int blockAll) {
                            System.out.printf("P:%d/%d\n",blockNo,blockAll);
                        }
                        @Override
                        public void onComplete(File dst) {

                        }
                    });
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void compose() {
        CCirnoLibraryComposer cCirnoLibraryComposer = new CCirnoLibraryComposer(
                "E:\\CIRNOS'DOC\\GITS\\ccirnos-library\\in",
                "E:\\CIRNOS'DOC\\GITS\\ccirnos-library-res\\lib"
        );
        cCirnoLibraryComposer.compose();
    }
}
